/**********************************Copyright (c)****************************
**                           
**------------------------------------File Info-----------------------------
** File name:               main.c
** Last modified date:      
** Last version:            
** Descriptions:
**---------------------------------------------------------------------------
** Created by:							zhehao.zhang
** Created date:						2020-12-11
** Version:									V1.00
** Descriptions:		
**----------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
*****************************************************************************/


#include "tinyOS.h"
#include "ARMCM3.h"


/*****************************************************************************
 *									宏定义
 ****************************************************************************/



/*****************************************************************************
 *									全局变量
 ****************************************************************************/

tTask *currentTask;
tTask *nextTask;
tTask *taskTable[2];

tTask tTask1;
tTask tTask2;

tTaskStack task1Env[1024];
tTaskStack task2Env[1024];


/*****************************************************************************
 *									函数
 ****************************************************************************/

void delay(int mTime)
{
	while(--mTime > 0);
}


/***************************************************************************************************
** Function name:           tTaskInit
** Descriptions:            初始化任务结构
** input parameters:        task： 要初始化的任务结构
** 							entry： 任务的入口函数
**							param： 传递给任务的运行参数	
** output parameters:      	none
** Returned value:          none
** Created by:              zheh.zhang
** Created date:            2020-12-30
**--------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
****************************************************************************************************/
void tTaskInit(tTask *task, void (*entry)(void *), void *param, uint32_t *stack)
{
	*(--stack) = (unsigned long)(1<<24);
	*(--stack) = (unsigned long)entry;
	*(--stack) = (unsigned long)0x14;
	*(--stack) = (unsigned long)0x12;
	*(--stack) = (unsigned long)0x3;
	*(--stack) = (unsigned long)0x2;
	*(--stack) = (unsigned long)0x1;
	*(--stack) = (unsigned long)param;
	*(--stack) = (unsigned long)0x11;
	*(--stack) = (unsigned long)0x10;
	*(--stack) = (unsigned long)0x9;
	*(--stack) = (unsigned long)0x8;
	*(--stack) = (unsigned long)0x7;
	*(--stack) = (unsigned long)0x6;
	*(--stack) = (unsigned long)0x5;
	*(--stack) = (unsigned long)0x4;

	task->stack = stack;
}


/***************************************************************************************************
** Function name:           tTaskSched
** Descriptions:            任务调度接口
** input parameters:       
** output parameters:      	none
** Returned value:          none
** Created by:              zheh.zhang
** Created date:            2020-01-04
**--------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
****************************************************************************************************/
void tTaskSched()
{
	if(currentTask == taskTable[0])
	{
		nextTask = taskTable[1];
	}
	else
	{
		nextTask = taskTable[0];
	}

	tTaskSwitch();
	
}

void tSetSysTickPeriod(uint32_t ms)
{
	SysTick->LOAD = ms*SystemCoreClock/1000 - 1;
	NVIC_SetPriority(SysTick_IRQn, (1<<__NVIC_PRIO_BITS) -1);
	SysTick->VAL = 0;
	SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
}

void SysTick_Handler()
{
	tTaskSched();
}


int task1Flag;
void task1(void *param)
{
	tSetSysTickPeriod(10);

	for(;;)
	{
		task1Flag = 1;
		delay(1000);

		task1Flag = 0;
		delay(1000);
	}
}

int task2Flag;
void task2(void *param)
{
	for(;;)
	{
		task2Flag = 1;
		delay(1000);

		task2Flag = 0;
		delay(1000);
	}
}



/***************************************************************************************************
** Function name:           main
** Descriptions:            查看全局变量变化
** input parameters:        
** output parameters:      	none
** Returned value:          none
** Created by:              zheh.zhang
** Created date:            2020-12-11
**--------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
****************************************************************************************************/
int main(void)
{
	tTaskInit(&tTask1, task1, (void *)0x11111111, &task1Env[1024] );
	tTaskInit(&tTask2, task2, (void *)0x22222222, &task2Env[1024] );

	taskTable[0] = &tTask1;
	taskTable[1] = &tTask2;

	nextTask = taskTable[0];

	tTaskRunFirst();
	
	return 0;
}




